package com.prb.factory;

import com.alibaba.druid.pool.DruidDataSource;
import com.prb.annotation.Autowrite;
import com.prb.annotation.Compoant;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Compoant
public class ConnectionFactory {


    public static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();
    
    @Autowrite
    DataSource dataSource;

    public Connection openConnection() throws SQLException {
        
        Connection connection = connectionThreadLocal.get();
        if(connection == null){
            connection = dataSource.getConnection();
            connectionThreadLocal.set(connection);
        }

        return connection;
    }
    
}
